package org.check.potimize.business;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.check.SQL.ConnectionPool;
import org.check.Util.LoadUtil;
import org.check.VO.DBParaVO;
import org.check.export.Export2Excel;
import org.check.multiThread.CntThread;
import org.check.optimize.VO.BaseCourseUser;

import jxl.write.WriteException;

public class LoadBaseCourseUser {
	
	static public ArrayList<BaseCourseUser> resultList = new ArrayList<BaseCourseUser>();
	static ConnectionPool connPool = null;
	static String filter = "";
	static public boolean isExport = false;
	static public List<String> pkList;
	static public boolean isError = false;
	public static void main(String[] args) {
		
		
	}

	public static void loadData(DBParaVO vo, List<String>selectedCol ,ConnectionPool connPool1, String filterCourse_id){
		List<String> tableNames = LoadUtil.getFileNameList(vo);	
		connPool = connPool1;
		filter = filterCourse_id;
		ResultSet rs = MapLoader.load(vo);
		resultList.removeAll(resultList);
		isExport = false;
		try {
			resultList = (ArrayList<BaseCourseUser>) MapLoader.resultSetToList(rs, BaseCourseUser.class);
			resultList = (ArrayList<BaseCourseUser>) mergeTeacherSameCourse(resultList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<String> selectedTableNames = new ArrayList<String>();
		Map<String, String> nameArrtMap = LoadUtil.getNameAttrMap();
		for(String tabName:tableNames){
			for(int i = 0; i < selectedCol.size(); i++){
				String colSelectedName = nameArrtMap.get(selectedCol.get(i));
				if(tabName.equalsIgnoreCase(colSelectedName))
					selectedTableNames.add(tabName);
			}
		}
		
		ExecutorService exe = Executors.newFixedThreadPool(50);
		List<CntThread> list = new ArrayList<CntThread>();
		for(String tabName:selectedTableNames){
			vo.setTablename(tabName);
			DBParaVO tmpVO = new DBParaVO();
			tmpVO = (DBParaVO)vo.clone();
			exe.execute(new CntThread(tmpVO));
		}
		exe.shutdown();
		while (!isExport) { 
			isExport = exe.isTerminated();
		}
	}
	
	public static List<BaseCourseUser> mergeTeacherSameCourse(List<BaseCourseUser> originalList){
		List<BaseCourseUser> result = new ArrayList<BaseCourseUser>();
		for(int i = 0; i < originalList.size(); i++){
			BaseCourseUser bcu = originalList.get(i);
			for(int j = i+1; j < originalList.size();){
				BaseCourseUser compBcu = originalList.get(j);
				if(bcu.getPk1().equals(compBcu.getPk1())){
					bcu.setUser_name(bcu.getUser_name()+","+compBcu.getUser_name());
					bcu.setUser_id(bcu.getUser_id()+","+compBcu.getUser_id());
					originalList.remove(compBcu);
				}
				else{
					j++;
				}
			}
			result.add(bcu);
		}
		return result;
	}
	
	public static void geneExcel(String dir, List<String>selectedCol){
		try {
			Export2Excel.export2Excel(dir, selectedCol, resultList);
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void loadData_geneExcel(DBParaVO vo, List<String>selectedCol ,ConnectionPool connPool1, String dir, String filterCourse_id) throws Exception{	
		List<String> tableNames = LoadUtil.getFileNameList(vo);	
		connPool = connPool1;
		filter = filterCourse_id;
		ResultSet rs = MapLoader.load(vo);
		try {
			resultList = (ArrayList<BaseCourseUser>) MapLoader.resultSetToList(rs, BaseCourseUser.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<String> selectedTableNames = new ArrayList<String>();
		Map<String, String> nameArrtMap = LoadUtil.getNameAttrMap();
		for(String tabName:tableNames){
			for(int i = 0; i < selectedCol.size(); i++){
				String colSelectedName = nameArrtMap.get(selectedCol.get(i));
				if(tabName.equalsIgnoreCase(colSelectedName))
					selectedTableNames.add(tabName);
			}
		}
		ExecutorService exe = Executors.newFixedThreadPool(50);
		for(String tabName:selectedTableNames){
			vo.setTablename(tabName);
			CntThread cntThread = new CntThread(vo);
			exe.execute(cntThread);
		}		
		boolean isExport = true;
		exe.shutdown();
		while (isExport) {  
			if(exe.isTerminated()){			
				isExport = false;
			}
		}
		 
	}
	
	
	public synchronized static void putValue2Obj(HashMap<String, String> map, String colName){
		Class cls = BaseCourseUser.class;
		Method[] methods = cls.getDeclaredMethods(); 
		String setMethodName = "set" + colName;
		for (int j = 0; j < methods.length; j++) { 
            if (methods[j].getName().equalsIgnoreCase(setMethodName)) 
                setMethodName = methods[j].getName(); 
        }	
		for(BaseCourseUser bcu:resultList){
			Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, String> entry = it.next();					
				if(bcu.getPk1() == null || entry.getKey() == null)
					continue;
				if(bcu.getPk1().toString().equals(entry.getKey().toString())){
					/*System.out.println("pk is "+bcu.getPk1().toString());
					System.out.println("value is "+entry.getValue());*/
					String value = entry.getValue().toString();
					Method setMethod;
					try {
						setMethod = bcu.getClass().getMethod( 
						        setMethodName, value.getClass());
						setMethod.invoke(bcu, value);
					} catch (NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
                    
				}
			    //System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
			}				
		}
	}
	
}
